package com.yjps.house.service;

import com.yjps.common.util.TimeUtil;
import com.yjps.house.dao.HistoryDao;
import com.yjps.house.dao.ShopDao;
import com.yjps.house.entity.ShopHistory;
import com.yjps.house.model.Shop;
import com.yjps.house.model.ShopApplyHistory;
import com.yjps.system.dao.AgentDao;
import com.yjps.system.dao.UserDao;
import com.yjps.system.model.Agent;
import com.yjps.system.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("shopService")
public class ShopService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private AgentDao agentDao;

    @Autowired
    private HistoryDao historyDao;

    @Autowired
    private ShopDao shopDao;

    /**
     * 用户申请门店
     * @param userId
     * @param reason
     * @param shop
     * @return
     */
    @Transactional
    public int applyShop(int userId,String reason,Shop shop){
        //保存门店
        shop.setUserId(userId);
        int shopId=shopDao.save(shop);
        String time=TimeUtil.getCurrentTimeInString(TimeUtil.DATE_FORMAT_DATE);
        //保存申请记录
        ShopApplyHistory history=new ShopApplyHistory();
        history.setApplyUserId(userId);
        history.setApplyReason(reason);
        history.setTime(time);
        history.setShopId(shopId);
        historyDao.save(history);

        return shopId;
    }

    /**
     * 系统管理员获取门店申请列表(未通过的)
     * @param startRow
     * @param pageSize
     * @return
     */
    @Transactional
    public List<ShopHistory> shopApplyList(int startRow,int pageSize){
        String hql="from t_shop_apply_history sh where sh.apply_hanlder_result = null and valid = 1";
        List<ShopApplyHistory> shs=historyDao.find(hql);
        List<ShopHistory> histories=new ArrayList<>();
        for (ShopApplyHistory sh:shs){
            Shop shop=shopDao.get(sh.getShopId());
            ShopHistory history=new ShopHistory(sh.getApplyUserId(),sh.getApplyReason(),sh.getTime(),shop);
            histories.add(history);
        }
        return histories.subList(startRow-1,startRow+pageSize-1);
    }

    /**
     * 系统管理员审核申请的门店
     * @param userId
     * @param shopId
     * @param result
     */
    @Transactional
    public void verifyShop(int userId,int shopId,int result){
        //查询申请记录
        String hql="from t_shop_apply_history sh where sh.shop_id =: shopId and valid =1";
        Map<String,Object> map=new HashMap<>();
        map.put("shopId", shopId);
        ShopApplyHistory history=(ShopApplyHistory)historyDao.find(hql, map).get(0);
        //更新申请记录
        String time= TimeUtil.getCurrentTimeInString(TimeUtil.DATE_FORMAT_DATE);
        history.setHandleTime(time);
        history.setHandleResult(result);
        history.setHandleUserId(userId);
        historyDao.update(history);
        //如果审核成功，更新用户角色信息
        if (result==1){
            //
        }
    }

    /**
     * 门店经理更新门店信息
     * 涉及门店关键信息的无法修改，例如营业执照等(暂时没有)
     * @param shop
     */
    @Transactional
    public boolean updateShop(Shop shop){
        Shop localShop=shopDao.get(shop.getId());
        if (localShop.getIsValid()==0||localShop.getState()==0){
            return false ;
        }
        shopDao.update(shop);
        return true;
    }

    /**
     * 门店经理关联一个经纪人
     * @param agent
     * @param userName
     * @return
     */
    public int addAgents(Agent agent,String userName){
        //关联UserId
        String hql="from t_user u where u.username =: userName";
        Map<String,Object> map=new HashMap<>();
        map.put("username",userName);
        User user=userDao.find(hql, map).get(0);
        agent.setUserId(user.getId());
        //保存agents
        int agentId=agentDao.save(agent);
        //如果关联成功 更新用户角色信息
        return agentId;
    }



}
